$(function () {
    var classroom_datagrid = $("#classroom_datagrid");
    var classroom_dialog = $("#classroom_dialog");
    var classroom_form = $("#classroom_form");
    var form_btns = $("#form_btns");
    classroom_datagrid.datagrid({
        columns: [[
            {field: 'id', title:"教室编号", hidden: true},
            {field: 'name', title: '教室名称', width: 50},
            {field: 'address', title: '教室地址', width: 50},
            {field: 'seatnumber', title: '座位数', width: 50},
            {
                field: 'state', title: '状态', width: 100, formatter: function (value, row, index) {
                return value ? "<font color='green'>可用</font>" : "<font color='red'>停用</font>";
            }
            },
        ]],
        fit: true,
        toolbar: '#toolbar',
        fitColumns: true,
        pagination: true,
        rownumbers: true,
        singleSelect: true,
        url: '/classroom/list',
        onClickRow:function (index, row) {
            if(row.state == 0){
                $("#changeState_btn").linkbutton({
                    text:"启用"
                })
            }else if(row.state == 1){
                $("#changeState_btn").linkbutton({
                    text:"停用"
                })
            }
        }
    })

    classroom_dialog.dialog({
        width: 390,
        height: 350,
        closed: true,
        closable: true,
        buttons: form_btns,
        onClose: function () {
            classroom_form.form("clear");
            /*//清空忆有权限的数据
             selfPermissions.datagrid('loadData', []);
             //重新加载所有权限的数据
             allPermissions.datagrid('load');*/
        },
    })

    var methodObj = {
        //添加
        add: function () {
            classroom_dialog.dialog("setTitle", "新增教室");
            classroom_dialog.dialog("open");
        },
        //编辑
        edit:function () {
            //获取选中的行
            var row = classroom_datagrid.datagrid("getSelected");
            if(row == null){
                $.messager.alert("提示","请选择需要修改的行");
            } else {
                //表单加载数据
                classroom_form.form("load", row);
                //设置弹窗标题
                classroom_dialog.dialog("setTitle", "修改教室");
                classroom_dialog.dialog("open");
            }
        },
        //保存更新

        // saveOrUpdate:function () {
        //     classroom_form.form('submit',{
        //         url:'/classroom/update',
        //         success:function (data) {
        //             var data = $.parseJSON(data);
        //             // var data = eval('(' + data + ')');
        //             if (data.success) {
        //                 $.messager.alert("提示", data.msg, "info", function () {
        //                     classroom_dialog.dialog(closed);   //关闭弹窗
        //                     classroom_datagrid.datagrid("reload");  //刷新当前列表
        //
        //                 })
        //             } else {
        //                 $.messager.alert("提示", data.msg, "error");  //保存或更新失败，弹窗提示
        //             }
        //         }
        //     })
        // }
        //保存操作
        saveOrUpdate:function () {
            var id = $("#classroomId").val();
            var url =  "/classroom/save";
            if(id){
                url =  "/classroom/update";
            }
            $("#classroom_form").form('submit', {
                url: url,
                success: function (data) {
                    data = $.parseJSON(data);

                    if(!data.success){
                        $.messager.alert('温馨提示',data.msg);

                    }else{
                        $.messager.alert('温馨提示',"操作成功");
                        //关闭框
                        classroom_dialog.dialog("close");
                        //刷新列表数据
                        classroom_datagrid.datagrid("load");
                    }
                }
            })
        },

        //修改教室状态
        changeState:function () {
            //获取选中的行
            var row = classroom_datagrid.datagrid("getSelected");
            if(row == null){
                $.messager.alert("提示",'请选中需要修改的行');
                return;
            }
            $.get('/classroom/changeState.do',{id:row.id},function (data) {
                if(data.success){
                    $.messager.alert("提示","操作成功","info",function () {
                        classroom_datagrid.datagrid("reload")
                    })
                } else {
                    $.messager.alert("提示","操作失败","error")
                }
            })
        },
        //刷新
        reload:function reload() {
            classroom_datagrid.datagrid("load");
        },
        //高级查询
        query: function () {
            //获取输入内容
            // var keyword = $("#keyword").textbox('getValue');
            var keyword = $("[name=keywords]").val();
            classroom_datagrid.datagrid("load", {
                keywords: keyword
            })
        }
    }
    $("a[data-cmd]").click(function () {
        //获取按键的data-cmd属性值
        var methodName = $(this).data('cmd');
        methodObj[methodName]();
    })

})